package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 向购物车增加套餐||菜品
     * @param shoppingCart
     * @param session
     * @return
     */
    //@Transactional
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session) {
        log.info(shoppingCart.toString());
        //设置用户id,指定当前是哪个用户的购物车数据 session和Thread皆可
        //shoppingCart.setUserId((Long) session.getAttribute("user"));
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart sc = shoppingCartService.getOne(lqw);
        //如果已经存在，就在原来数量基础上加一
        if (sc != null) {
            /*LambdaUpdateWrapper<ShoppingCart> shoppingCartUpdateWrapper = new LambdaUpdateWrapper<>();
            shoppingCartUpdateWrapper.set(ShoppingCart::getNumber,sc.getNumber()+1);
            shoppingCartUpdateWrapper.eq(ShoppingCart::getId,sc.getId());
            shoppingCartService.update(shoppingCartUpdateWrapper);*/
            //return R.success();此处踩坑为返回shoppingCart应同一返回
            //本来的思想：因mybatis底层，map存放键值，根据址引用也会修改实体类数据
            //坑：上面使用updatewrapper并没有改变原实体类
            sc.setNumber(sc.getNumber()+1);
            shoppingCartService.updateById(sc);
            return R.success(sc);
        }
        shoppingCart.setNumber(1);//此处虽插入数据库默认值为1,但由于返回前端也要有值所以还需要设置
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     *查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);
    }

    /**
     * 清理购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clear() {
        shoppingCartService.remove(null);
        return R.success("清除购物车成功");
    }

    /**
     * 减少购物车商品数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> update(@RequestBody ShoppingCart shoppingCart) {
        //创建条件构造器
        LambdaQueryWrapper<ShoppingCart> sclw = new LambdaQueryWrapper<>();
        sclw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        sclw.eq(shoppingCart.getDishId()!=null,ShoppingCart::getDishId,shoppingCart.getDishId());
        sclw.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        //获取购物车菜品或套餐
        ShoppingCart sc = shoppingCartService.getOne(sclw);
        //判断数量
        if (sc.getNumber() == 1) {
            //删除该商品
            shoppingCartService.removeById(sc);
            sc.setNumber(0);
            return R.success(sc);
        }
        //更新数量
        sc.setNumber(sc.getNumber()-1);
        shoppingCartService.updateById(sc);
        return R.success(sc);
    }
}
